package entity

import (
	"strings"

	"gitee.com/linxing_3/sr-core/domain/model"
)

func NewSysApiBatch(ids ...int) []any {
	res := make([]any, len(ids))
	for i, id := range ids {
		res[i] = &SysApi{
			GAIntPK: model.GAIntPK{
				Id: id,
			},
		}
	}
	return res
}

type SysApi struct {
	model.GAIntPK

	Path    string `json:"path" form:"path" gorm:"index;size:256;comment:路径"`
	Method  string `json:"method" form:"method" gorm:"index;size:16;comment:方法"`
	Title   string `json:"title" form:"title" gorm:"size:256;comment:标题"`
	Group   string `json:"group" form:"group" gorm:"size:256;comment:分组"`
	Handler string `json:"handler" form:"handler" gorm:"size:256;comment:处理器"`
}

func (SysApi) TableName() string {
	return "sys_api"
}

func (s *SysApi) SetGroup(g string) {
	if g != "" {
		s.Group = g
	} else {
		p := strings.TrimLeft(s.Path, "/")
		paths := strings.Split(p, "/")
		if len(paths) <= 2 {
			s.Group = ""
			return
		} else if len(paths) > 3 {
			s.Group = paths[2] + "/" + paths[3]
		} else {
			s.Group = paths[2]
		}
	}
}
